﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrainTechLLC.FolderExplorer;
using ImageAppDataModels;
using DirectoryViewModels;
using System.IO;
using System.Windows.Threading;
using BrainTechLLC;
using System.ComponentModel;

namespace StandaloneFolderExplorer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        DirectoriesToExamineViewModel _vm;
        public static Window1 Window;

        public Window1()
        {
            InitializeComponent();
            Window = this;
            Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        public static void WriteLog(string message)
        {
            Window.Dispatcher.BeginInvoke(new VoidDelegate(() => Window.logControl.AddMessage(message)));
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            folderExplorer.OperationHandler.FindFoldersToDeleteStart += new EventHandler<ObjectEventArgs<FolderViewModel>>(OperationHandler_OnFindFoldersToDeleteStart);
            folderExplorer.OperationHandler.FindFoldersToDeleteEnd += new EventHandler<ObjectEventArgs<FolderViewModel>>(OperationHandler_OnFindFoldersToDeleteEnd);
            folderExplorer.OperationHandler.SelectFoldersStart += new EventHandler<ObjectEventArgs<FolderViewModel>>(OperationHandler_OnSelectFoldersStart);
            folderExplorer.OperationHandler.SelectFoldersEnd += new EventHandler<ObjectEventArgs<FolderViewModel>>(OperationHandler_OnSelectFoldersEnd);
            DirectoriesToExamine dirsToExamine = new DirectoriesToExamine();
            dirsToExamine.Add(new DirectoryToExamine("C:\\"));
            dirsToExamine.Add(new DirectoryToExamine("C:\\Owen"));
            dirsToExamine.Add(new DirectoryToExamine("C:\\Stratopia"));
            _vm = new DirectoriesToExamineViewModel(dirsToExamine);
            folderExplorer.RootFolder = new FolderViewModel(_vm, null);
            var f = folderExplorer.RootFolder;
            f.PropertyChanged += new PropertyChangedEventHandler(f_PropertyChanged);
            f.ClearSubfolders += new EventHandler(f_ClearSubfolders);
            f.SubfoldersPopulated += new EventHandler(f_SubfoldersPopulated);
            WireSubfolders(f);
            cboFolders.ItemsSource = f.SpecialFolders;
            this.DataContext = f;            
        }

        private void WireSubfolders(FolderViewModel f)
        {
            if (f.SubFoldersCached != null)
            {
                foreach (var folder in f.SubFoldersCached)
                {
                    folder.PropertyChanged += new PropertyChangedEventHandler(f_PropertyChanged);
                    folder.ClearSubfolders += new EventHandler(f_ClearSubfolders);
                    folder.SubfoldersPopulated += new EventHandler(f_SubfoldersPopulated);
                }
            }
        }

        void f_SubfoldersPopulated(object sender, EventArgs e)
        {
            FolderViewModel f = sender as FolderViewModel;
            WireSubfolders(f);
        }

        void f_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            FolderViewModel f = sender as FolderViewModel;
            //WriteLog(string.Format("FolderViewModel {0}: {1}", f.Name, e.PropertyName));
        }

        void f_ClearSubfolders(object sender, EventArgs e)
        {
            FolderViewModel f = sender as FolderViewModel;
            UnwireSubfolders(f);
        }

        private void UnwireSubfolders(FolderViewModel f)
        {
            if (f.SubFoldersCached != null)
            {
                foreach (var folder in f.SubFoldersCached)
                {
                    folder.SubfoldersPopulated -= f_SubfoldersPopulated;
                    folder.ClearSubfolders -= f_ClearSubfolders;
                    folder.PropertyChanged -= f_PropertyChanged;
                }
            }
        }

        void OperationHandler_OnSelectFoldersStart(object sender, ObjectEventArgs<FolderViewModel> e)
        {            
            WriteLog(string.Format("Starting Selections on {0}", e.obj.Name));
        }

        void OperationHandler_OnFindFoldersToDeleteEnd(object sender, ObjectEventArgs<FolderViewModel> e)
        {
            WriteLog(string.Format("Finding Folders to Delete End on {0}", e.obj.Name));
        }

        void OperationHandler_OnSelectFoldersEnd(object sender, ObjectEventArgs<FolderViewModel> e)
        {
            WriteLog(string.Format("Ending Selections on {0}", e.obj.Name));
        }

        void OperationHandler_OnFindFoldersToDeleteStart(object sender, ObjectEventArgs<FolderViewModel> e)
        {
            WriteLog(string.Format("Finding Folders to Delete Start on {0}", e.obj.Name));
        }

        private void mnuExit_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void cboFolders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cboFolders.SelectedItem != null)
            {
                string folderName = cboFolders.SelectedItem as string;
                Console.WriteLine(cboFolders.SelectedItem.ToString());
                folderExplorer.RootFolder.SelectedSpecialFolder = folderName;
            }
        }

        private void mnuInMemory_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Directories: " + folderExplorer.CountDirectoriesInMemory().ToString());
            sb.AppendLine("Files: " + folderExplorer.CountFilesInMemory().ToString());
            MessageBox.Show(sb.ToString());
        }

        private void mnuShowAllDirSizes_Click(object sender, RoutedEventArgs e)
        {
            folderExplorer.ShowAllDirSizes();
        }
    }
}
